package com.xiaotu.spider.guduo.pageprocessor;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.PageUtils;
import us.codecraft.webmagic.Page;



/***
 * 骨朵网剧分集播放量任务
 * @author Mr.zhang
 *
 */
@Component("GuDuoNetTVSetsPlayCountPageProcessor")
public class GuDuoNetTVSetsPlayCountPageProcessor extends GuDuoSetsPlayCountPageProcessor {

	private Map<String,String> distinctDataidMap = new HashMap<String,String>();
	
	private String [] business_url = new String[] {
			
			"http://ev.guduodata.com/pro/m/platform/list?category=NETWORK_DRAMA&dateType=DAILY&day=%s&platformId=1&orderTitle=playCount" //腾讯视频
			,
			"http://ev.guduodata.com/pro/m/platform/list?category=NETWORK_DRAMA&dateType=DAILY&day=%s&platformId=2&orderTitle=hotCount" //爱奇艺
			,
			"http://ev.guduodata.com/pro/m/platform/list?category=NETWORK_DRAMA&dateType=DAILY&day=%s&platformId=3&orderTitle=hotCount" //优酷
			,
			"http://ev.guduodata.com/pro/m/platform/list?category=NETWORK_DRAMA&dateType=DAILY&day=%s&platformId=5&orderTitle=playCount" //搜狐视频
			,
			"http://ev.guduodata.com/pro/m/platform/list?category=NETWORK_DRAMA&dateType=DAILY&day=%s&platformId=7&orderTitle=playCount" //芒果TV
	};
	
	
	private String startDate = DateUtils.getSpecifiedDayBefore(
			DateUtils.getCurrentDate(new Date(), DateUtils.DAY_FORMAT), DateUtils.DAY_FORMAT
			);
	
	private String endDate = startDate;
	
	@Override
    public void initRunParams(Map<String, Object> map)
    {
        if(map.get("startDate") != null)
        	this.startDate = map.get("startDate").toString();
        if(map.get("endDate") != null)
        	this.endDate = map.get("endDate").toString();
    }
	
	@Override
	protected void addBusinessProcess(Page page) {
		
		String currDate = startDate;
		
		while(true){
			
			synchronized(this){
				if(currDate.equals(endDate)){
					distinctDataIdMethod(page,business_url,currDate);
					break;
				}else{
					distinctDataIdMethod(page,business_url,currDate);
				}
				currDate = DateUtils.getSpecifiedDayAfterDate(currDate,DateUtils
			            .DAY_FORMAT);
				
				System.out.println(currDate);
				
			}
			
		}
		
	}
	
	@SuppressWarnings("unchecked")
	private void distinctDataIdMethod(Page page,String[] urlArr,String currDate){
		
		for(String url : urlArr){
			String jSONP = HttpUtils.httpGetJSONP(String.format(url, currDate),super.headers);
			Map<String, Object> everyMap = GsonUtils.fromJson(jSONP, Map.class);
			List<Map<String,Object>> dataList =  (List<Map<String,Object>>)everyMap.get("data");
			if(null == dataList || dataList.isEmpty()){
				continue;
			}
			for(Map<String,Object> map : dataList){
				String tv_dataid = map.get("showId").toString();
				tv_dataid = tv_dataid.replace(".0", "");
				String tvname = map.get("name")+"";
				if(!distinctDataidMap.containsKey(tv_dataid)){
					Map<String,Object> extrasMap = new HashMap<String,Object>();
					extrasMap.put("tv_dataid", tvname);
					extrasMap.put("tvname", tv_dataid);
					PageUtils.addTargetRequest(page, String.format(super.play_count_url, tv_dataid), extrasMap);
					distinctDataidMap.put(tv_dataid, tvname);
				}
			}
		}
	}
	
}
